package com.snow.useviewdemo.function2.radius;

import android.content.Context;
import android.graphics.Color;

import com.snow.useviewdemo.R;


/**
 * Desc:  颜色选择器
 */
public class ColorPickGradient {
    //设置的颜色
    private int[] PICK_YOU_COLORS;
    private int[] PICK_LIANG_COLORS;
    private int[] PICK_QINGDU_COLORS;
    private int[] PICK_MODELDU_COLORS;
    private int[] PICK_ZHONGDU_COLORS;
    private int[] PICK_YANZHONG_COLORS;
    //每个颜色的位置
    private float[] PICKCOLORBAR_POSITIONS;

    private int[] mColorArr;
    private float[] mColorPosition;




    public ColorPickGradient(int[] colorArr) {
        this.mColorArr = colorArr;
    }

    public ColorPickGradient(Context context) {

        PICK_YOU_COLORS = new int[]{context.getResources().getColor(R.color.air_quality_progress_you_start),
                context.getResources().getColor(R.color.air_quality_progress_you_end)};
        PICK_LIANG_COLORS = new int[]{context.getResources().getColor(R.color.air_quality_progress_liang_start),
                context.getResources().getColor(R.color.air_quality_progress_liang_end)};
        PICK_QINGDU_COLORS = new int[]{context.getResources().getColor(R.color.air_quality_progress_qingdu_start),
                context.getResources().getColor(R.color.air_quality_progress_qingdu_end)};
        PICK_MODELDU_COLORS = new int[]{context.getResources().getColor(R.color.air_quality_progress_model_start),
                context.getResources().getColor(R.color.air_quality_progress_model_end)};
        PICK_ZHONGDU_COLORS = new int[]{context.getResources().getColor(R.color.air_quality_progress_zhongdu_start),
                context.getResources().getColor(R.color.air_quality_progress_zhongdu_end)};
        PICK_YANZHONG_COLORS = new int[]{context.getResources().getColor(R.color.air_quality_progress_yanzhong_start),
                context.getResources().getColor(R.color.air_quality_progress_yanzhong_end)};

        PICKCOLORBAR_POSITIONS = new float[]{0f, 1f};
        mColorPosition = PICKCOLORBAR_POSITIONS;
    }

    /**
     * 获取某个百分比位置的颜色
     * @param radio 取值[0,1]
     * @param end
     * @return
     */
    public int getColor(float origRadio, float totalRatio) {
        int startColor;
        int endColor;
        float radio = getShaderByPercent(origRadio, totalRatio);
        if (radio >= 1) {
            return mColorArr[mColorArr.length - 1];
        }
        for (int i = 0; i < mColorPosition.length; i++) {
            if (radio <= mColorPosition[i]) {
                if (i == 0) {
                    return mColorArr[0];
                }
                startColor = mColorArr[i - 1];
                endColor = mColorArr[i];
                float areaRadio = getAreaRadio(radio,mColorPosition[i-1],mColorPosition[i]);
                return getColorFrom(startColor,endColor,areaRadio);
            }
        }
        return -1;
    }

    /**
     * 获取某个百分比位置的颜色
     */
    public int getColorBg(float total) {
        if (total <= 0.1) {
            //优
            return Color.parseColor("#8000EF9E");
        } else if (total <= 0.2) {
            //良
            return Color.parseColor("#80FFC82D");
        } else if (total <= 0.3) {
            //轻度污染
            return Color.parseColor("#80FFA42E");
        }else if (total <= 0.4) {
            //中度污染
            return Color.parseColor("#80FF9000");
        }else if (total <= 0.6) {
            //重度污染
            return Color.parseColor("#80FF5B07");
        } else{
            //严重污染
            return Color.parseColor("#80FE0A09");
        }
    }

    /**
     * 外阴影，注：颜色必须要有透明度alpha，否则阴影不显示
     * @param total
     * @return
     */
    public int getOutShadowColorBg(float total) {
        float value = 0.167f;
        if (total <= value) {
            //优
            return Color.parseColor("#9900EF9E");
        } else if (total <= value * 2) {
            //良
            return Color.parseColor("#99FFC82D");
        } else if (total <= value * 3) {
            //轻度污染
            return Color.parseColor("#99FFA42E");
        }else if (total <= value * 4) {
            //中度污染
            return Color.parseColor("#99FF9000");
        }else if (total <= value * 5) {
            //重度污染
            return Color.parseColor("#99FF5B07");
        } else{
            //严重污染
            return Color.parseColor("#99FE0A09");
        }
    }

    /**
     * 外阴影，注：颜色必须要有透明度alpha，否则阴影不显示
     * @return
     */
    public float getPercent(int index) {
        if (index == 1) {
            //优
            return 0.167f;
        } else if (index == 2) {
            //良
            return 0.2f;
        } else if (index == 3) {
            //轻度污染
            return 0.3f;
        }else if (index == 4) {
            //中度污染
            return 0.4f;
        }else {
            //重度污染
            return 0.6f;
        }
    }



    public float getAreaRadio(float radio, float startPosition, float endPosition) {
        return (radio - startPosition) / (endPosition - startPosition);
    }

    /**
     *  取两个颜色间的渐变区间 中的某一点的颜色
     * @param startColor
     * @param endColor
     * @param radio
     * @return
     */
    public int getColorFrom(int startColor, int endColor, float radio) {
        int redStart = Color.red(startColor);
        int blueStart = Color.blue(startColor);
        int greenStart = Color.green(startColor);
        int redEnd = Color.red(endColor);
        int blueEnd = Color.blue(endColor);
        int greenEnd = Color.green(endColor);

        int red = (int) (redStart + ((redEnd - redStart) * radio + 0.5));
        int greed = (int) (greenStart + ((greenEnd - greenStart) * radio + 0.5));
        int blue = (int) (blueStart + ((blueEnd - blueStart) * radio + 0.5));
        return Color.argb(255, red, greed, blue);
    }

    /**
     * 0f, 0.2f, 0.4f, 0.6f, 0.8f, 1f
     * @param ratio
     * @return
     */
    public float getShaderByPercent(float ratio, float total) {
        float value = 0.167f;
        if (total <= 0.1567f) {
            //优
            mColorArr = PICK_YOU_COLORS;
            value = 0.1567f;
        } else if (total <= 0.3233f) {
            mColorArr = PICK_LIANG_COLORS;
            value = 0.3233f;
        } else if (total <= 0.494f) {
            mColorArr = PICK_QINGDU_COLORS;
            value = 0.494f;
        }else if (total <= 0.6567f) {
            mColorArr = PICK_MODELDU_COLORS;
            value = 0.6567f;
        }else if (total <= 0.8253f) {
            mColorArr = PICK_ZHONGDU_COLORS;
            value = 0.8253f;
        } else{
            mColorArr =  PICK_YANZHONG_COLORS;
            value = 1f;
        }
        return getRealPercent(0, value, ratio);
    }

    private float getRealPercent(float start, float end, float current) {
        return (current - start) / (end - start);
    }

}
